package blog.datasource;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class CatalogTests {
	@Autowired
	private Catalog catalog;

	public void setCatalog(Catalog catalog) {
		this.catalog = catalog;
	}

	@Test
	public void testDataSourceRouting() {
		CustomerContextHolder.setCustomerType(CustomerType.GOLD);
		List<Item> goldItems = catalog.getItems();
		assertEquals(3, goldItems.size());
		System.out.println("gold items: " + goldItems);

		CustomerContextHolder.setCustomerType(CustomerType.SILVER);
		List<Item> silverItems = catalog.getItems();
		assertEquals(2, silverItems.size());
		System.out.println("silver items: " + silverItems);

		CustomerContextHolder.clearCustomerType();
		List<Item> bronzeItems = catalog.getItems();
		assertEquals(1, bronzeItems.size());
		System.out.println("bronze items: " + bronzeItems);
	}
}

//test data:
//	gold items: [gold item #1 (250.0), gold item #2 (325.45), gold item #3 (55.6)]
//	silver items: [silver item #1 (25.0), silver item #2 (15.3)]
//	bronze items: [bronze item #1 (23.75)]